

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>toydata.PriorityQueue &mdash; ToyData 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> ToyData
          

          
          </a>

          
            
            
              <div class="version">
                1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../Intro.html">Introductions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">toydata</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ToyData</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>toydata.PriorityQueue</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for toydata.PriorityQueue</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">toydata.utils</span> <span class="kn">import</span> <span class="n">Empty</span>
<span class="kn">from</span> <span class="nn">toydata.PositionalList</span> <span class="kn">import</span> <span class="n">PositionalList</span>


<span class="c1"># Composition design pattern</span>
<div class="viewcode-block" id="PriorityQueueBase"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.PriorityQueueBase">[docs]</a><span class="k">class</span> <span class="nc">PriorityQueueBase</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Abstract base class for a priority queue&quot;&quot;&quot;</span>

    <span class="k">class</span> <span class="nc">_Item</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Lightweight composite to store priority queue items&quot;&quot;&quot;</span>
        <span class="vm">__slots__</span> <span class="o">=</span> <span class="s1">&#39;_key&#39;</span><span class="p">,</span> <span class="s1">&#39;_value&#39;</span>

        <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_key</span> <span class="o">=</span> <span class="n">k</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_value</span> <span class="o">=</span> <span class="n">v</span>

        <span class="c1"># compare items based on there keys</span>
        <span class="k">def</span> <span class="fm">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_key</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">_key</span></div>


<div class="viewcode-block" id="UnsortedPriorityQueue"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.UnsortedPriorityQueue">[docs]</a><span class="k">class</span> <span class="nc">UnsortedPriorityQueue</span><span class="p">(</span><span class="n">PriorityQueueBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A min-oriented priority queue implemented with an unsorted list&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new empty Priority Queue&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="n">PositionalList</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rep</span> <span class="o">=</span> <span class="s1">&#39;{&#39;</span>
        <span class="n">rep</span> <span class="o">+=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;({str(item._key)}, {str(item._value)})&#39;</span>
                         <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>
        <span class="n">rep</span> <span class="o">+=</span> <span class="s1">&#39;}&#39;</span>
        <span class="k">return</span> <span class="n">rep</span>

<div class="viewcode-block" id="UnsortedPriorityQueue.is_empty"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.UnsortedPriorityQueue.is_empty">[docs]</a>    <span class="k">def</span> <span class="nf">is_empty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the queue is empty&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span></div>

    <span class="k">def</span> <span class="nf">_find_min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return Position of item with minimum key&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">Empty</span><span class="p">(</span><span class="s1">&#39;Priority queue is empty&#39;</span><span class="p">)</span>
        <span class="n">small</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
        <span class="n">walk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">after</span><span class="p">(</span><span class="n">small</span><span class="p">)</span>
        <span class="k">while</span> <span class="n">walk</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">walk</span><span class="o">.</span><span class="n">element</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">small</span><span class="o">.</span><span class="n">element</span><span class="p">():</span>
                <span class="n">small</span> <span class="o">=</span> <span class="n">walk</span>
            <span class="n">walk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">after</span><span class="p">(</span><span class="n">walk</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">small</span>

<div class="viewcode-block" id="UnsortedPriorityQueue.add"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.UnsortedPriorityQueue.add">[docs]</a>    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a key-val pair&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">add_last</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_Item</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">))</span></div>

<div class="viewcode-block" id="UnsortedPriorityQueue.min"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.UnsortedPriorityQueue.min">[docs]</a>    <span class="k">def</span> <span class="nf">min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return but do not remove (k, v) tuple with minimum key&quot;&quot;&quot;</span>
        <span class="n">p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_min</span><span class="p">()</span>
        <span class="n">item</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">element</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">_key</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">_value</span><span class="p">)</span></div>

<div class="viewcode-block" id="UnsortedPriorityQueue.remove_min"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.UnsortedPriorityQueue.remove_min">[docs]</a>    <span class="k">def</span> <span class="nf">remove_min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove and return (k. v) tuple with minimum key&quot;&quot;&quot;</span>
        <span class="n">p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_min</span><span class="p">()</span>
        <span class="n">item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">_key</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">_value</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="SortedPriorityQueue"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.SortedPriorityQueue">[docs]</a><span class="k">class</span> <span class="nc">SortedPriorityQueue</span><span class="p">(</span><span class="n">PriorityQueueBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A min-oriented priority queue implemented with a sorted list&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new empty Priority Queue&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="n">PositionalList</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of items in the priority queue&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rep</span> <span class="o">=</span> <span class="s1">&#39;{&#39;</span>
        <span class="n">rep</span> <span class="o">+=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;({str(item._key)}, {str(item._value)})&#39;</span>
                         <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>
        <span class="n">rep</span> <span class="o">+=</span> <span class="s1">&#39;}&#39;</span>
        <span class="k">return</span> <span class="n">rep</span>

<div class="viewcode-block" id="SortedPriorityQueue.is_empty"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.SortedPriorityQueue.is_empty">[docs]</a>    <span class="k">def</span> <span class="nf">is_empty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the queue is empty&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="SortedPriorityQueue.add"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.SortedPriorityQueue.add">[docs]</a>    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a key-value pair&quot;&quot;&quot;</span>
        <span class="n">newest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_Item</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="n">walk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">last</span><span class="p">()</span>
        <span class="k">while</span> <span class="n">walk</span> <span class="ow">and</span> <span class="n">newest</span> <span class="o">&lt;</span> <span class="n">walk</span><span class="o">.</span><span class="n">element</span><span class="p">():</span>
            <span class="n">walk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">before</span><span class="p">(</span><span class="n">walk</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">walk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">add_first</span><span class="p">(</span><span class="n">newest</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">add_after</span><span class="p">(</span><span class="n">walk</span><span class="p">,</span> <span class="n">newest</span><span class="p">)</span></div>

<div class="viewcode-block" id="SortedPriorityQueue.min"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.SortedPriorityQueue.min">[docs]</a>    <span class="k">def</span> <span class="nf">min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return but do not remove (k, v) tuple with minimum key&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">Empty</span><span class="p">(</span><span class="s1">&#39;Priority queue is empty&#39;</span><span class="p">)</span>
        <span class="n">p</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
        <span class="n">item</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">element</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">_key</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">_value</span><span class="p">)</span></div>

<div class="viewcode-block" id="SortedPriorityQueue.remove_min"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.SortedPriorityQueue.remove_min">[docs]</a>    <span class="k">def</span> <span class="nf">remove_min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove and return (k, v) tuple with minimum key&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">Empty</span><span class="p">(</span><span class="s1">&#39;Priority queue is empty&#39;</span><span class="p">)</span>
        <span class="n">item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">first</span><span class="p">())</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">_key</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">_value</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="HeapPriorityQueue"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.HeapPriorityQueue">[docs]</a><span class="k">class</span> <span class="nc">HeapPriorityQueue</span><span class="p">(</span><span class="n">PriorityQueueBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A min-oriented priority queue implemented with a binary heap&quot;&quot;&quot;</span>

    <span class="c1"># public data</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Create a new empty Priority Queue&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of items in the priority queue&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">rep</span> <span class="o">=</span> <span class="s1">&#39;{&#39;</span>
        <span class="n">rep</span> <span class="o">+=</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;({str(item._key)}, {str(item._value)})&#39;</span>
                         <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>
        <span class="n">rep</span> <span class="o">+=</span> <span class="s1">&#39;}&#39;</span>
        <span class="k">return</span> <span class="n">rep</span>

<div class="viewcode-block" id="HeapPriorityQueue.is_empty"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.HeapPriorityQueue.is_empty">[docs]</a>    <span class="k">def</span> <span class="nf">is_empty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if priority list is empty&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span></div>

<div class="viewcode-block" id="HeapPriorityQueue.add"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.HeapPriorityQueue.add">[docs]</a>    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a key-value pair to the priority list&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_Item</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_upheap</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span></div>

<div class="viewcode-block" id="HeapPriorityQueue.min"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.HeapPriorityQueue.min">[docs]</a>    <span class="k">def</span> <span class="nf">min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">Empty</span><span class="p">(</span><span class="s1">&#39;Priority queue is empty&#39;</span><span class="p">)</span>
        <span class="n">item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">_key</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">_value</span><span class="p">)</span></div>

<div class="viewcode-block" id="HeapPriorityQueue.remove_min"><a class="viewcode-back" href="../../toydata.html#toydata.PriorityQueue.HeapPriorityQueue.remove_min">[docs]</a>    <span class="k">def</span> <span class="nf">remove_min</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove and return (k ,v) tuple with minimum key.</span>
<span class="sd">        Raise Empty exception if empty&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_empty</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">Empty</span><span class="p">(</span><span class="s1">&#39;Priority queue is empty&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">item</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_downheadp</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">_key</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">_value</span><span class="p">)</span></div>

    <span class="c1"># non-public behaviors</span>
    <span class="k">def</span> <span class="nf">_parent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>

    <span class="k">def</span> <span class="nf">_left</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">_right</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">j</span> <span class="o">+</span> <span class="mi">2</span>

    <span class="c1"># check index(beyond end or not)</span>
    <span class="k">def</span> <span class="nf">_has_left</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_left</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_has_right</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_right</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_swap</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Swap the elements at indices i and j of array&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_upheap</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parent</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">j</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">parent</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_swap</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="n">parent</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_upheap</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_downheadp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_left</span><span class="p">(</span><span class="n">j</span><span class="p">):</span>
            <span class="n">left</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_left</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
            <span class="c1"># although right may be smaller</span>
            <span class="n">small_child</span> <span class="o">=</span> <span class="n">left</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_has_right</span><span class="p">(</span><span class="n">j</span><span class="p">):</span>
                <span class="n">right</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_right</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">right</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">left</span><span class="p">]:</span>
                    <span class="n">small_child</span> <span class="o">=</span> <span class="n">right</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">small_child</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="p">[</span><span class="n">j</span><span class="p">]:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_swap</span><span class="p">(</span><span class="n">small_child</span><span class="p">,</span> <span class="n">j</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_downheadp</span><span class="p">(</span><span class="n">small_child</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Xiangzhuang Shen

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>